﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;

namespace AUV5.Web
{
    public class PageBaseTop : AUV5.Common.PageBase
    {
        protected override void OnPreInit(EventArgs e)
        {
            //base.OnPreInit 触发 Page_PreInit
            base.OnPreInit(e);
            //this.Theme = "Default";

        }

        protected override void OnPreLoad(EventArgs e)
        {
            //base.OnPreLoad 触发 Page_PreLoad
            base.OnPreLoad(e);
        }




        #region Session相关

        #region 提示信息

        /// <summary>
        /// 错误提示
        /// </summary>
        public string ErrorInfo
        {
            get
            {
                if (Session["ErrorInfo"] != null)
                {
                    return Session["ErrorInfo"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                Session["ErrorInfo"] = value;
            }
        }

        /// <summary>
        /// 成功提示
        /// </summary>
        public string SuccessInfo
        {
            get
            {
                if (Session["SuccessInfo"] != null)
                {
                    return Session["SuccessInfo"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                Session["SuccessInfo"] = value;
            }
        }

        /// <summary>
        /// 警告
        /// </summary>
        public string NoticeInfo
        {
            get
            {
                if (Session["NoticeInfo"] != null)
                {
                    return Session["NoticeInfo"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                Session["NoticeInfo"] = value;
            }
        }

        #endregion
        /// <summary>
        /// 用户登录后的Session信息
        /// 创建人：Amos Wnn
        /// 创建时间：2011-10-14
        /// </summary>
        public AUV5.Model.VerifiedInfo VerifiedInfo
        {
            get
            {
                Model.VerifiedInfo verifieduser = Session["UserInfo"] as AUV5.Model.VerifiedInfo;
                //未激活的用户，每次刷新都读取一次用户信息
                if (verifieduser != null)
                {
                    if (verifieduser.User.State == (int)Common.Enum.auvState.Disable)
                    {
                        verifieduser = new Model.VerifiedInfo(BLL.UserInfoBLL.Instance.GetDetail(verifieduser.User.UserID));
                        return verifieduser;
                    }
                    else
                    {
                        return verifieduser;
                    }
                }
                else
                {
                    return null;
                }
            }
            set
            {
                Session["UserInfo"] = value;
            }
        }

        /// <summary>
        /// 用户登录后的Session信息
        /// 创建人：Amos Wnn
        /// 创建时间：2011-10-14
        /// </summary>
        public bool IsVerified
        {
            get
            {
                return VerifiedInfo == null ? false : true;
            }
        }

        /// <summary>
        /// 返回登录用户的ID
        /// </summary>
        public string VerifiedUserID
        {
            get
            {
                if (IsVerified)
                {
                    return VerifiedInfo.User.UserID;
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 增值服务功能是否启用
        /// </summary>
        public bool ExtraService_Enabled
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["extraservice_enable"] == "true";
            }
        }
        #endregion

        #region 弹框相关

        /// <summary>
        /// JS弹框
        /// 创建人：Amos Wnn
        /// 创建时间：2011-10-14
        /// </summary>
        /// <param name="page"></param>
        /// <param name="context"></param>
        public void Alert(string alertContext)
        {
            this.AlertGo(alertContext, null, null);
        }

        /// <summary>
        /// JS弹框并跳转
        /// 创建人：Amos Wnn
        /// 创建时间：2011-10-14
        /// </summary>
        /// <param name="page"></param>
        /// <param name="context"></param>
        public void AlertGo(string alertContext, string url)
        {
            this.AlertGo(alertContext, url, null);
        }

        //关闭当前页并刷新父页面
        public void CloseAndRefreshOpener()
        {
            string js = @"self.window.opener.focus();
                        self.window.opener.location.href = self.window.opener.location.href;
                        var opened = window.open('','_self');
                        opened.opener = null;
                        opened.close();";
            this.AlertGo("", "", js);
        }

        //关闭thickbox并刷新父页面
        public void CloseThickBox(bool refresh)
        {
            if (refresh)
            {
                string js = @"parent.location.href=parent.location.href;
                          parent.$.fn.colorbox.close()";
                this.AlertGo("", "", js);
            }
            else
            {
                this.AlertGo("", "", "parent.$.fn.colorbox.close()");
            }
        }

        /// <summary>
        /// 弹出登录框
        /// </summary>
        //public void Login()
        //{
        //    string js = @"login('/Testing/Login.htm')";
        //    this.AlertGo("", "", js);
        //}


        /// <summary>
        /// JS弹框并跳转追加其他js
        /// 创建人：Amos Wnn
        /// 创建时间：2011-10-14
        /// </summary>
        /// <param name="page"></param>
        /// <param name="context"></param>
        /// <param name="url"></param>
        public void AlertGo(string alertContext, string url, string otherScripts)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (!string.IsNullOrEmpty(alertContext))
            {
                sb.AppendFormat("alert(\"{0}\");", alertContext);
            }

            if (!string.IsNullOrEmpty(url))
            {
                sb.AppendFormat("location.href=\"{0}\";", url);
            }

            if (!string.IsNullOrEmpty(otherScripts))
            {
                sb.AppendFormat("{0};", otherScripts);
            }

            Page.ClientScript.RegisterStartupScript(this.GetType(), "", sb.ToString(), true);
        }

        #endregion

        #region 下拉列表年份设置
        /// <summary>
        /// 下拉列表设置年份
        /// 创建人： Amos Wnn
        /// 创建时间：2011-08-15
        /// </summary>
        /// <param name="listControl"></param>
        public void SetDropDownListYear(ListControl listControl)
        {
            int yearScope = 15;
            for (int yearBegin = DateTime.Now.Year - yearScope; yearBegin < DateTime.Now.Year + yearScope; yearBegin++)
            {
                listControl.Items.Add(new ListItem(yearBegin.ToString(), yearBegin.ToString()));
            }

            listControl.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
        }
        #endregion

        #region##设置省市下拉列表
        /// <summary>
        /// 设置省市下拉列表
        /// 创建人：Wilson
        /// 创建时间：2011-10-23
        /// </summary>
        /// <param name="lc"></param>
        /// <param name="pCode"></param>
        public void SetGBAD(ListControl lc, string pCode)
        {
            IList<AUV5.Model.GBAD> list = AUV5.BLL.GBADBLL.GetInstance().GetListByADParentCode(pCode);
            lc.DataSource = list;
            lc.DataTextField = "ADName";
            lc.DataValueField = "ADID";
            lc.DataBind();
        }
        #endregion

        #region 获取日期目录
        public string GetDirectoryOfDate(bool isTemp)
        {
            string upload = "UploadFile";

            string temp = "Temp/User";
            if (!isTemp)
                temp = "User";

            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.ToString("MM");
            string day = DateTime.Now.ToString("dd");

            string[] paras = new string[] { upload, temp, year, month, day };

            string directory = string.Format("/{0}/{1}/{2}/{3}/{4}/", paras);

            return directory;
        }
        #endregion
    }
}